import 'package:flutter/material.dart';
import 'package:flutter_nb_net/flutter_net.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:tracker/pages/home/Feedback/model/feedback_list_resp.dart';

import '../../../../common/constants/app_constants.dart';
import '../../../../common/log/x_logger.dart';
import '../../../../common/widget/common.dart';
import '../../../../theme/app_theme.dart';
import '../../../../utils/color_utils.dart';
import '../../../../utils/toast_utils.dart';
import '../model/feedback_detail_resp.dart';

/// 反馈 页面
class FeedbackDetailPage extends ConsumerStatefulWidget {
  const FeedbackDetailPage({super.key, required this.feedbackId});
  final int feedbackId;

  @override
  ConsumerState<FeedbackDetailPage> createState() => _FeedbackDetailPageState();
}

class _FeedbackDetailPageState extends ConsumerState<FeedbackDetailPage> {

  bool showLoading = true;
  int pageNum = 1;
  FeedbackListData? data;



  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      getFeedbackDetail();
    });
    super.initState();

  }

  @override
  void dispose() {
    super.dispose();
  }
  Widget _buildDeviceStatus(int status) {
    switch (status) {
      case 0:
        return _buildDeviceStatusView('待处理', '#909399');
      case 1:
        return _buildDeviceStatusView('已处理', '#19be6b');
      default:
        return _buildDeviceStatusView('未激活', '#c8c9cc');
    }
  }
  Widget _buildDeviceStatusView(String text, String color) {
    return Container(
      margin: const EdgeInsets.only(left: 10),
      padding: const EdgeInsets.only(left: 10, right: 10, top: 2, bottom: 2),
      decoration: BoxDecoration(
        color: ColorUtils.hexToColor(color),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Text(
        text,
        style: const TextStyle(
          color: Colors.white,
          fontSize: 12,
        ),
      ),
    );
  }
  Future<void> getFeedbackDetail() async {
    XLogger.info(widget.feedbackId);
    //
    // final queryParams = {
    //   'feedbackId': widget.feedbackId,
    // };
    final res = await get<FeedbackDetailResp, FeedbackDetailResp>('$newBaseULr/feedback/${widget.feedbackId}',
        queryParameters: null, decodeType: FeedbackDetailResp());
    res.when(success: (FeedbackDetailResp resp) {
      if (resp.code == 200) {
        data = resp.data;
      } else {
        ToastUtils.showToast(resp.msg ?? '反馈详情获取失败');
      }
      setState(() {
        showLoading = false;
      });
    }, failure: (String msg, int code) {
      XLogger.error('获取设备列表失败 code: $code msg: $msg');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('反馈详情', style: TextStyle(fontSize: 18)),
          centerTitle: true,
          backgroundColor: AppTheme.themeConfig.navBarColor,
          foregroundColor: AppTheme.themeConfig.primaryColor,
          leading: GestureDetector(
            onTap: () {
              Navigator.pop(context);
            },
            child: Container(
              height: kToolbarHeight,
              padding: const EdgeInsets.only(top: 8),
              alignment: Alignment.center,
              child: const Text(' <返回',
                  textAlign: TextAlign.center,
                  maxLines: 1,
                  style: TextStyle(color: Colors.black)),
            ),
          ),
        ),
        backgroundColor: AppTheme.themeConfig.navBarColor,
        body: Center(
          child: Container(
            width: MediaQuery.of(context).size.width * 0.9,
            margin: const EdgeInsets.symmetric(vertical: 20),
            padding: const EdgeInsets.all(16),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(12),
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.2),
                  spreadRadius: 2,
                  blurRadius: 5,
                  offset: const Offset(0, 3),
                ),
              ],
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '${data?.subject}',
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                    color: AppTheme.themeConfig.primaryTextColor,
                  ),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
                const SizedBox(height: 16),
                Align(
                  alignment: Alignment.centerRight,
                  child: Container(
                    padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
                    decoration: BoxDecoration(
                      color: (data?.status ?? 0) == 1 ? Colors.green : Colors.red,
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: Text(
                      (data?.status ?? 0) == 1 ? '已处理' : '未处理',
                      style: const TextStyle(color: Colors.white, fontSize: 12),
                    ),
                  ),
                ),
                const SizedBox(height: 16),
                const Divider(height: 1, color: Colors.grey),
                const SizedBox(height: 16),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      '创建时间：',
                      style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.bold,
                        color: AppTheme.themeConfig.primaryTextColor,
                      ),
                    ),
                    Expanded(
                      child: Text(
                        data?.createTime ?? ' ',
                        style: TextStyle(
                          fontSize: 14,
                          color: AppTheme.themeConfig.secondaryTextColor,
                        ),
                        maxLines: null,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      '反馈内容：',
                      style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.bold,
                        color: AppTheme.themeConfig.primaryTextColor,
                      ),
                    ),
                    Expanded(
                      child: Text(
                        data?.content ?? ' ',
                        style: TextStyle(
                          fontSize: 14,
                          color: AppTheme.themeConfig.secondaryTextColor,
                        ),
                        maxLines: null,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                if ((data?.processing ?? "").isNotEmpty)
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '处理结果：',
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                          color: AppTheme.themeConfig.primaryTextColor,
                        ),
                      ),
                      Expanded(
                        child: Text(
                          data?.processing ?? ' ',
                          style: TextStyle(
                            fontSize: 14,
                            color: AppTheme.themeConfig.secondaryTextColor,
                          ),
                          maxLines: null,
                        ),
                      ),
                    ],
                  ),
                const SizedBox(height: 8),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      '更新时间：',
                      style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.bold,
                        color: AppTheme.themeConfig.primaryTextColor,
                      ),
                    ),
                    Expanded(
                      child: Text(
                        data?.updateTime ?? ' ',
                        style: TextStyle(
                          fontSize: 14,
                          color: AppTheme.themeConfig.secondaryTextColor,
                        ),
                        maxLines: null,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      'IMEI：',
                      style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.bold,
                        color: AppTheme.themeConfig.primaryTextColor,
                      ),
                    ),
                    Expanded(
                      child: Text(
                        data?.imei ?? ' ',
                        style: TextStyle(
                          fontSize: 14,
                          color: AppTheme.themeConfig.secondaryTextColor,
                        ),
                        maxLines: null,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 8),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      '发布者联系方式：',
                      style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.bold,
                        color: AppTheme.themeConfig.primaryTextColor,
                      ),
                    ),
                    Expanded(
                      child: Text(
                        data?.contacts ?? ' ',
                        style: TextStyle(
                          fontSize: 14,
                          color: AppTheme.themeConfig.secondaryTextColor,
                        ),
                        maxLines: null,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
    );
  }
}
